﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.App_Logic.Dummy;
using NRCP.BusinessLogicLayer;
using NRCP.Helpers;

namespace NRCP
{
    public partial class Plan_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            BindGrid(0, SiteSettings.PageSize);
            BindPager(0, SiteSettings.PageSize);

           
        }

        protected void repPager_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            BindGrid(Convert.ToInt32(e.CommandArgument)-1, SiteSettings.PageSize);
        }

        private void BindPager(int currentPageIndex, int pageSize)
        {
            NationalPlanBLL bll = new NationalPlanBLL();
            double totalCount = 0;
            Int32 pageCount = 0;
            totalCount = bll.GetTotalCountForAllNationalPlans("Id", currentPageIndex, pageSize);
            pageCount = Convert.ToInt32(Math.Ceiling(totalCount / pageSize));
            if (pageCount <= 0)
            {
                pageCount = 1;
            }

            litTotalRecordCount.Text = totalCount.ToString();
            repPager.DataSource = Enumerable.Range(1, pageCount).ToList();
            repPager.DataBind();
        }

        private void BindGrid(int currentPageIndex, int pageSize)
        {
            currentPageIndex = currentPageIndex * pageSize;
            NationalPlanBLL bll = new NationalPlanBLL();
            grvPlans.DataSource = bll.GetAllNationalPlansPaged("Id",currentPageIndex, pageSize);
            grvPlans.DataBind();
        }
    }
}